﻿using NewAndong.Comm;
using NewAndong.Model;
using Sunny.UI;
using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewAndong.Forms
{
    public partial class FrmUnpackingProgress : UIPage
    {
        AutoSizeFormClass asc = new AutoSizeFormClass();
        bool isLoad = true;
        CancellationTokenSource cancel_KKStatuscontrol;

        public FrmUnpackingProgress()
        {
            InitializeComponent();
        }

        private void FrmUnpackingProgress_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;
            isLoad = false;

            asc.controllInitializeSize(this);
            Init_Control();

            //开启线程绑定数据
            cancel_KKStatuscontrol = new CancellationTokenSource();
            Task.Factory.StartNew(() =>
            {
                KKStatusCommand();
            }, cancel_KKStatuscontrol.Token);
        }

        private void FrmUnpackingProgress_SizeChanged(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
        }

        private void Init_Control()
        {
            for (int i = 1; i < 30; i++)
            {
                var control = this.Controls.Find("uiLabel" + i, true);
                if (control != null && control.Length > 0)
                {
                    UILabel label = control[0] as UILabel;
                    label.ForeColor = Color.White;
                }
            }

            for (int i = 1; i < 10; i++)
            {
                var control = this.Controls.Find("labelPart" + i, true);
                if (control != null && control.Length > 0)
                {
                    UILabel label = control[0] as UILabel;
                    label.ForeColor = Color.White;
                }
            }

            foreach (UnpackingDirectionShowModel model in GolbalVariable.UnpackingDirectionModelBag)
            {
                var control = this.Controls.Find("labelPart" + model.TypeId, true);
                if (control != null && control.Length > 0)
                {
                    Label la = control[0] as Label;
                    la.Text = model.UnpackingName.ToString();
                }
            }
        }

        /// <summary>
        /// 开捆指示更新
        /// </summary>
        private async void KKStatusCommand()
        {
            while (!this.cancel_KKStatuscontrol.Token.IsCancellationRequested)
            {
                foreach (UnpackingDirectionShowModel model in GolbalVariable.UnpackingDirectionModelBag)
                {
                    UpdateUI(model);
                }
                await Task.Delay(1000);
            }
        }

        private void UpdateUI(UnpackingDirectionShowModel unpackingDirectionModel)
        {
            if (string.IsNullOrEmpty(unpackingDirectionModel.ControlName))
                return;
            this.Invoke(new MethodInvoker(() =>
            {
                var control = this.Controls.Find(unpackingDirectionModel.ControlName, true);
                if (control != null && control.Length > 0)
                {
                    Label la = control[0] as Label;
                    la.Text = unpackingDirectionModel.UnpackingCount.ToString();
                }
            }));
        }
    }
}
